package data_structure.rbtree001.data;

import java.io.Serializable;

/**
 * 红黑树节点
 */
public class Node implements Serializable {
    /* 节点的值 */
    private Integer value = null;
    /* 节点颜色是否是红 */
    private boolean isRed = true;

    /* 父节点 */
    private Node parent;
    /* 左节点 */
    private Node left;
    /* 右节点 */
    private Node right;

    public Node getParent() {
        return parent;
    }
    public Node(Integer value) {
        this.value = value;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public boolean isRed() {
        return isRed;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        if(null != left) left.parent = this;
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        if(null != right) right.parent = this;
        this.right = right;
    }

    public void setRed(boolean red) {
        isRed = red;
    }

    // 取得兄弟节点
    public Node getBrother(){
        Node parent = this.getParent();
        if(null == parent) return null;
        if (this == parent.getLeft()) {
            return parent.getRight();
        }else {
            return parent.getLeft();
        }
    }

    // 是否是左节点
    public boolean isLeftNode(){
        Node parent = this.getParent();
        if(null == parent) return false;
        return parent.getLeft() == this;
    }
    @Override
    public String toString() {
        return "Node{" + value + " - " + (isRed?"红色":"黑色")+"}" + (null != parent? (" --- 父节点:" + parent.value) : "");
    }
}
